package com.tg.service;

import com.tg.service.child.Customer;
import com.tg.service.event.MessageEvent;
import com.tg.service.suzakuEventBus.handler.MqEventHandler;
import com.tg.service.suzakuEventBus.pojo.RuntimeEvent;
import com.tg.service.utils.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@RestController
@SpringBootApplication
//@EnableDiscoveryClient
//@Import({
//        FooImportBeanDefinitionRegistrar.class
//})
@ImportResource("classpath:engine.xml")
@EnableSwagger2
@PropertySource("classpath:swagger.properties")
public class Application {
    public static void main(String[] args) {
//        SpringApplication.run(Application.class, args);

        ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
        ContextUtil.setApplicationContext(ctx);


//        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(TaskEventHandler.class);
//        BeanDefinition beanDefinition = builder.getBeanDefinition();
//        beanDefinition.setAutowireCandidate(true);
//        beanDefinition.setLazyInit(false);
//        builder.addPropertyValue("eventName", "Login");
////        registry.registerBeanDefinition(eventName + TaskEventHandler.class.getSimpleName(), beanDefinition);
//
//        //获取BeanFactory
//        DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) ctx.getAutowireCapableBeanFactory();
//        defaultListableBeanFactory.registerBeanDefinition("Login" + TaskEventHandler.class.getSimpleName(), beanDefinition);
//
//        TaskEventHandler handler2 = ContextUtil.getBean("Login" + TaskEventHandler.class.getSimpleName());
//
//        adapter.setEventBusListener(Lists.newArrayList(handler2));
//        adapter.goon();
    }

    @Autowired
    private MqEventHandler adapter;

    @RequestMapping(value = "/index")
    public String index() {
        MessageEvent event = new MessageEvent("tg");


//        EventPublisher.publishEvent(event);

//        adapter.post(event);

        System.out.println("Spring in Action:" + adapter);
        return "Spring in Action";
    }


    @RequestMapping(value = "/index2")
    public String index2() {

        RuntimeEvent runtimeEvent = new RuntimeEvent();
        runtimeEvent.setEventName("login");
        adapter.post(runtimeEvent);

        System.out.println("Spring in Action:" + adapter);
        return "Spring in Action";
    }

    @RequestMapping(value = "/index3")
    public String index3() {

        Customer customer = ContextUtil.getBean("Customer");


        System.out.println("Spring in Action:" + customer.getName());
        return "Spring in Action";
    }

    @RequestMapping(value = "/single")
    public String single() {
        return "hello tg";
    }


}
